﻿using Anno.Rpc;
using Anno.Rpc.Client;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.AspNetCore
{
    public class DefaultSelectRoute : ISelectRoute
    {
        /// <summary>
        /// 根据用户get参数city ，选取一个服务昵称中包含city的服务
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Micro SelectRoute(HttpContext context)
        {
            string nickName = string.Empty;
            var routeValues = context.Request.RouteValues;
            routeValues.TryGetValue("channel", out object channel);
            return Single(channel.ToString())?.Mi;
        }
        private static MicroCache Single(string channel)
        {
            List<MicroCache> list = Connector.Micros.ToList().FindAll((MicroCache m) => m.Tags.Exists((string t) => t == channel));
            Random random = new Random(Guid.NewGuid().GetHashCode());
            if (list.Count > 0)
            {
                return list[random.Next(0, list.Count)];
            }

            return null;
        }
    }
}
